//
//  PostImagesViewController.m
//  ZhuanZuBao
//
//  Created by 戴伟 on 2020/9/5.
//  Copyright © 2020 戴伟. All rights reserved.
//

#import "PostImagesViewController.h"
#import "TZImagePickerController.h"
#import "PostPicCell.h"

@interface PostImagesViewController ()<TZImagePickerControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, PostPicCellDelegate>

@property (nonatomic, strong) UIImage *mainImage;
@property (nonatomic, strong) NSMutableArray *imageArr;
@property (weak, nonatomic) IBOutlet UIImageView *mainImageView;
@property (weak, nonatomic) IBOutlet UIButton *uploadImageBtn;
@property (weak, nonatomic) IBOutlet UIButton *deleteBtn;

@property (nonatomic, assign) BOOL isChooseMainImage;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;

@property (nonatomic,  strong) UICollectionView *collectionView;
@end

@implementation PostImagesViewController

-(instancetype)initWithMainImage:(UIImage *)mainImage imageArr:(NSArray *)imageArr{
    if (self = [super init]) {
        if (mainImage) {
            _mainImage = mainImage;
        }

        _imageArr = [NSMutableArray array];
        if (imageArr) {
            [_imageArr addObjectsFromArray:imageArr];
        }
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _uploadImageBtn.hidden = YES;
    
    self.title  = @"编辑图片";
    
    _mainImageView.image = _mainImage;
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [UILabel new];
    [_mainImageView addSubview:label];
    label.text = @"首图";
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor whiteColor];
    label.backgroundColor = [UIColor colorWithHexString:@"#000000" andAlpha:0.4];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.height.mas_equalTo(30);
    }];
    
    
    UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc] init];
    fl.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 230, DEVICE_SCREEN_WIDTH, 280) collectionViewLayout:fl];
    _collectionView.contentInset = UIEdgeInsetsMake(10, 10, 0, 10);
    [_collectionView registerClass:[PostPicCell class] forCellWithReuseIdentifier:@"cell"];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_collectionView];
    
}

- (IBAction)chooseMainImage:(id)sender {
    _isChooseMainImage = YES;
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
        imagePickerVc.allowPickingVideo = NO;
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto{
    if (_isChooseMainImage) {
        _mainImageView.image = photos[0];
        _mainImage = photos[0];
        _deleteBtn.hidden = NO;
        _uploadImageBtn.hidden = YES;
        _isChooseMainImage = NO;
    }else{
        [self.imageArr addObjectsFromArray:photos];
        [self.collectionView reloadData];
    }
    
}

- (IBAction)submit:(id)sender {
    if (!_mainImage) {
        [MBProgressHUD showWarning:@"请选择主图" toView:nil];
        return;
    }
    if (self.selectImageBlock) {
        self.selectImageBlock(_mainImage, [_imageArr copy]);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)deleteMainImage:(id)sender {
    _mainImageView.image = [UIImage imageNamed:@"postImageBg"];
    _mainImage = nil;
    _deleteBtn.hidden = YES;
    _uploadImageBtn.hidden = NO;
}

#pragma mark - UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.imageArr.count == 3) {
        return self.imageArr.count;
    }
    return self.imageArr.count+1;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PostPicCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.delegate = self;
    if (indexPath.row < self.imageArr.count ) {
        cell.image = self.imageArr[indexPath.row];
    }else{
        cell.image = nil;
    }
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat w = (DEVICE_SCREEN_WIDTH-40)/3;
    return CGSizeMake(w, w);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self.view endEditing:YES];
    if (indexPath.row == self.imageArr.count) {
        //继续添加图片
        TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:3-self.imageArr.count delegate:self];
        imagePickerVc.allowPickingVideo = NO;
        [self presentViewController:imagePickerVc animated:YES completion:nil];
    }else{
        //浏览大图
        
    }
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 15;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}

#pragma mark - 删除图片
-(void)deletePic:(UIImage *)pic{
    [self.imageArr removeObject:pic];
    [self.collectionView reloadData];
}


@end
